package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dto.page.MyPage;
import com.itheima.dto.yddUser.SysUserInfo;
import com.itheima.entity.YddUser;
import com.itheima.service.YddUserService;
import com.itheima.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 会员管理 用户管理
 *
 * @author zhangpei
 * @ClassName UserController
 * @date 2022/5/31/16:03
 */
@RequestMapping("/ydd_user")
@RestController
@Slf4j
public class UserController {

    @Autowired
    private YddUserService yddUserService;

    @GetMapping("/list")
    public R<MyPage<YddUser>> list(Integer sex, String username, String mobile, Integer page, Integer limit) {
        Page<YddUser> yddUserPage = new Page<>(page, limit);
        LambdaQueryWrapper<YddUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(sex != null, YddUser::getSex, sex)
                .like(username != null, YddUser::getUsername, username)
                .like(mobile != null, YddUser::getMobile, mobile);
        yddUserService.page(yddUserPage, wrapper);


        //设置MyPage的基本属性
        MyPage<YddUser> myPage = new MyPage<>();
        //总条数
        myPage.setCount(yddUserPage.getTotal());
        //每页条数
        myPage.setPageSize(yddUserPage.getSize());
        //总页数
        myPage.setTotalPages((yddUserPage.getTotal() / yddUserPage.getSize() + 1));
        //当前页
        myPage.setCurrentPage(yddUserPage.getCurrent());
        myPage.setData(yddUserPage.getRecords());
        return R.success(myPage);
    }

    /**
     * 用户添加
     *
     * @param yddUser
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody YddUser yddUser) {
        yddUserService.save(yddUser);
        return R.success(null);
    }

    /**
     * 删除用户
     *
     * @return
     */
    @GetMapping("/del")
    public R<String> delete(Integer id) {
        yddUserService.removeById(id);
        return R.success(null);
    }

    /**
     * 修改数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<YddUser> rowInfo(Integer id) {
        YddUser yddUser = yddUserService.getById(id);
        return R.success(yddUser);
    }

    /**
     * 会员管理 修改用户
     * @param yddUser
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody YddUser yddUser) {
        yddUserService.updateById(yddUser);
        return R.success(null);
    }
}
